// Michael Depuy
// depuym@onid.orst.edu
// CS311-400
// Homework 3

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ar.h>
#include <fcntl.h>

#define NAME 16
#define DATE 12
#define UID 6
#define GID 6
#define MODE 8
#define SIZE 10
#define FMAG 2
#define HEADER 60

// Format: myar -<key> <archive file> <members...>

/*
  struct stat{
  
  uid_t st_uid;
  gid_t st_gid;
  off_t st_size;
  time_t st_mtime;
  mode_t st_mode;
  
};

  struct ar_hdr{
    char ar_name[16];
    char ar_date[12];
    char ar_uid[6], ar_gid[6];
    char ar_mode[8];
    char ar_size[10];
    char ar_fmag[2];
  };
*/

int file_check(const char *file){
  
  return 0;
}

int main(int argc,char **argv){
  
  struct ar_hdr *ar_file;
  struct stat stat_file;
  int opt;
  int fd;
  int writeto,readfrom;
  time_t time;
  struct tm *timebroken;
  char *ascii_time;
  char *timestr;
  int i,j,k;

  while((opt = getopt(argc,argv,"q:x:t:v:d:A:")) != -1){
    switch(opt){
    case 'q':
      fd = open(optarg, O_CREAT | O_EXCL);
      write(fd,ARMAG,SARMAG);
      close(fd);
      break;

    case 'x':
      break;

    case 't':
      fd = open(optarg, O_RDONLY);
      lseek(fd,SARMAG,SEEK_SET);
      
      while(read(fd,ar_file,HEADER) == 60){
        for(i=0; i<16; i++){
          if(ar_file->ar_name[i] == '/'){
            break;
          }
        printf("%c",ar_file->ar_name[i]);
        }
      }

      break;

    case 'v':
      
      fd = open(optarg, O_RDONLY);
      lseek(fd,SARMAG,SEEK_SET);
      printf("\nIn -v \n");
      int temp = read(fd,ar_file,HEADER);
      printf("%d\n",temp);
      while(read(fd,ar_file,HEADER) == 60){
        printf("In while loop\n");
      	// Print permissions -- StackOverflow search result
      	stat(ar_file->ar_name,&stat_file);
        printf( (stat_file.st_mode & S_IRUSR) ? "r" : "-");
        printf( (stat_file.st_mode & S_IWUSR) ? "w" : "-");
        printf( (stat_file.st_mode & S_IXUSR) ? "x" : "-");
        printf( (stat_file.st_mode & S_IRGRP) ? "r" : "-");
        printf( (stat_file.st_mode & S_IWGRP) ? "w" : "-");
        printf( (stat_file.st_mode & S_IXGRP) ? "x" : "-");
        printf( (stat_file.st_mode & S_IROTH) ? "r" : "-");
        printf( (stat_file.st_mode & S_IWOTH) ? "w" : "-");
        printf( (stat_file.st_mode & S_IXOTH) ? "x" : "-");
        printf(" ");
      	// Print UID/GID
      	printf("%d/",atoi(ar_file->ar_uid));
        printf("%d\t",atoi(ar_file->ar_gid));
      	// Print then file size
      	printf("%d ",atoi(ar_file->ar_size));
      	
      	// Print date/time of last modification (mtime())
      	// Month Day Time Year
        timebroken = localtime(&stat_file.st_mtime);
        strftime(timestr,100,"%b %e %H:%M %Y",timebroken);
        puts(timestr);
        printf(" ");
        // Print filename
        for(i=0; i<16; i++){
          if(ar_file->ar_name[i] == '/')
            break;
          printf("%c",ar_file->ar_name[i]);
        }

      	lseek(fd,atoi(ar_file->ar_size)+1,SEEK_CUR);
        printf("\n");
      }
      break;

    case 'd':
      break;

    case 'A':
      break;

    default:
      break;
    }
  }

  printf("\n");
  return 0;
    
}
